package cheme.ProcessEditor.Table;

import cheme.ProcessEditor.Parts.Streams.Tag.TableRowData;

public class SubTableLayout extends TableLayout implements ITableLayout, ITableElementLayout
{
	private ITableLayout parent = null;
	private TableRowData actualRowData = null;
	private TableRowData adjustedRowData = null;
		
	public SubTableLayout(ITableLayout parent)
	{
		super(null);
		setParent(parent);
	}
	
	public ITableLayout getParent()
	{
		return parent;
	}
	public void setParent(ITableLayout parent)
	{
		this.parent = parent;
	}

	public TableRowData getAdjustedRowData()
	{
		return adjustedRowData;
	}
	
	public TableRowData getActualRowData()
	{
		return actualRowData;
	}
	
	public void setAdjustedRowData(TableRowData rowData)
	{
		this.adjustedRowData = rowData;
	}
	
	public void setActualRowData(TableRowData rowData)
	{
		this.actualRowData = rowData;
	}
	
	/*
	 * The idea here is that the sub table runs the max row data calc on it's children, 
	 * and reports this to the parent table layout.
	 * Then, when layout's occur, subtables will delegate their getRowData to the parent table.
	 */
	
	public void updateMaxRowData(ITableElementLayout source)
	{
//		System.out.println("TableLayout.updateMaxRowData -- start");
		
		TableRowData newRowData = calculateMaxRowData(source);
		
		if(null == getRowData() || !getRowData().equals(newRowData))
		{
			setActualRowData(newRowData);
			
			if(null != getParent())
			{
				getParent().updateMaxRowData(this);
			}
		}

//		System.out.println("TableLayout . updateMaxRowData -- end");
	}
	
	@Override
	public TableRowData getRowData()
	{
		if(null != getParent())
		{
			return getParent().getRowData();
		}
		
		return getAdjustedRowData();
	}
}
